package top.fangwenmagician.calculationstring.log;

import java.util.LinkedList;

/**
 * 默认日志对象
 */
public class DefaultLog implements I_CalculationLog {
    /**
     * 运算的表达式
     */
    private String expression;
    /**
     * 日志列表
     */
    private final LinkedList<String> logList;

    public DefaultLog() {
        logList = new LinkedList<>();
    }

    public DefaultLog(String expression) {
        this.expression = expression;
        logList = new LinkedList<>();
    }

    /**
     * 设置运算表达式
     *
     * @param expression 表达式
     */
    public void startLog(String expression) {
        this.expression = expression;
    }

    @Override
    public void writeLog(String logText) {
        if (logText != null && !logText.isEmpty()) {
            logList.add(logText);
        }
    }

    /**
     * 删除并返回第一条记录
     *
     * @return 删除的第一条记录
     * @throws Exception 可能出现的错误
     */
    public String removeFirst() throws Exception {
        return logList.removeFirst();
    }

    @Override
    public String removeLast() {
        return logList.removeLast();
    }

    /**
     * 输出日志
     *
     * @return 日志
     */
    @Override
    public String toString() {
        StringBuilder logBuilder = new StringBuilder();
        logBuilder.append("  ")
                .append(expression)
                .append("\n");
        for (String log : logList) {
            logBuilder.append("= ")
                    .append(log)
                    .append("\n");
        }
        return logBuilder.toString();
    }
}
